//
//  PopoverController.swift
//  
//
//  Created by youzy01 on 2021/3/31.
//

import UIKit

private let popverCellId = "popverCellId"

public class PopverController: UIViewController {
    @IBOutlet private weak var tableView: UITableView!

    public var sourceView: UIView? {
        didSet {
            popoverPresentationController?.sourceView = sourceView
        }
    }

    public var sourceRect: CGRect = .zero {
        didSet {
            popoverPresentationController?.sourceRect = sourceRect
        }
    }

    var permittedArrowDirections: UIPopoverArrowDirection = .up {
        didSet {
            popoverPresentationController?.permittedArrowDirections = permittedArrowDirections
        }
    }

    private let items: [String]
    private let complation: ((Int) -> Void)?

    public init(items: [String], complation: @escaping (Int) -> Void) {
        self.items = items
        self.complation = complation
        super.init(nibName: "PopverController", bundle: .module)
        modalPresentationStyle = .popover
        popoverPresentationController?.delegate = self
        popoverPresentationController?.permittedArrowDirections = .up
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    public override func viewDidLoad() {
        super.viewDidLoad()

        tableView.register(UITableViewCell.self, forCellReuseIdentifier: popverCellId)

        let width = items.map { $0.size(of: 16).width }.max() ?? 150
        var height = CGFloat(items.count * 44)
        if items.count > 10 {
            height = CGFloat(10 * 44)
            tableView.isScrollEnabled = true
        }
        preferredContentSize = CGSize(width: width + 50, height: height)
    }
}

extension PopverController: UITableViewDataSource {
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: popverCellId, for: indexPath)
        cell.textLabel?.textAlignment = .center
        cell.textLabel?.text = items[indexPath.row]
        cell.textLabel?.font = UIFont.systemFont(ofSize: 16)
        return cell
    }
}

extension PopverController: UITableViewDelegate {
    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        dismiss(animated: true, completion: nil)
        complation?(indexPath.row)
    }
}

extension PopverController: UIPopoverPresentationControllerDelegate {
    public func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        return .none
    }
}

extension String {
    func size(of font: CGFloat) -> CGSize {
        let attributes: [NSAttributedString.Key: Any] = [
            NSAttributedString.Key.font: UIFont.systemFont(ofSize: font)
        ]
        return NSAttributedString(string: self, attributes: attributes).size()
    }
}
